#!/bin/bash
# bootstrap.sh  hosts.list
set -e
[[ $1 ]] || { echo "Usage: $0 <host_file>"; exit 1; }
INSTALLER="$(cd "$(dirname "$0")" && pwd)/install.sh"
[[ -f $INSTALLER ]] || { echo "install.sh not found"; exit 1; }
command -v sshpass >/dev/null || dnf -y install sshpass

SSH_PORT=1122        # ← 1. 新增端口变量

while read -r ip; do
  [[ -z "$ip" || ${ip:0:1} == "#" ]] && continue
  echo ">>> $ip"
  {
    # 2. 推送公钥
    sshpass -e ssh-copy-id -o StrictHostKeyChecking=no -p "$SSH_PORT" root@$ip 2>/dev/null || true
    # 3. 拷贝脚本
    scp -o StrictHostKeyChecking=no -P "$SSH_PORT" "$INSTALLER" root@$ip:/root/install.sh
    # 4. 远程执行
    ssh -o StrictHostKeyChecking=no -p "$SSH_PORT" root@$ip "bash /root/install.sh"
  } &
done < "$1"
wait
echo "All nodes done."